<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="../../../plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../../css/normalize.css"/>
    <link rel="stylesheet" href="../../../css/fa.css"/>
    <link rel="stylesheet" href="../../../css/icon.css"/>
    <link rel="stylesheet" href="../../../css/ares_animate.min.css"/>
    <link rel="stylesheet" href="../../../css/ares_hover_variant.min.css"/>
    <link rel="stylesheet" href="../../../css/ares_reboot.css"/>
    <link rel="stylesheet" href="../../../css/app.min.css"/>
    <link rel="stylesheet" href="../../../css/iframe.css"/>
    <style>

    </style>

</head>
<body>
<form class="page-search layui-form" lay-filter="search-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">治疗单类别</label>
            <div class="layui-input-inline">
                <select id="type-id" name="typeId">
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">套餐名称</label>
            <div class="layui-input-inline">
                <input name="searchKey" placeholder="请输入套餐名称" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="search-submit">查询</button>
        </div>
        <div class="layui-inline">
            <button class="layui-btn create-btn">新建</button>
        </div>
    </div>
</form>
<table class="layui-hide" id="table" lay-filter="table"></table>
<div id="pagination"></div>
</body>
<script type="text/html" id="tableBar">
    <button class="layui-btn layui-btn-xs" lay-event="update">更新</button>
    <button class="layui-btn layui-btn-xs layui-btn-danger ares-mr" lay-event="remove">删除</button>
    {{#  if(d.disable){ }}
    <input type="checkbox" name="status" lay-skin="switch" data-combo-id={{d.id}} lay-filter="status">
    {{#  } else { }}
    <input type="checkbox" name="status" lay-skin="switch" checked data-combo-id={{d.id}} lay-filter="status">
    {{#  } }}
</script>
<script type="text/javascript" src="../../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../../aresExtends/config/layuiConfig.js"></script>
<script>
    layui.use(['table', 'form', 'laypage', 'conf', 'utils', 'comboServices', 'mandyServices', '_'], function (table, form, laypage, config, utils, comboServices, mandyServices, _) {
        var $createBtn = $('.create-btn'),
            $typeId = $('#type-id'),
            typeList = [];

        init();

        /**
         * @author: ares
         * @date: 2021/3/9 9:26
         * @description: 监听更新列表事件
         */
        utils.aresPageEventInit('updateComboList', 'combo', function () {
            getPageList(1, config.pagination.limit);
        })

        /**
         * @author: ares
         * @date: 2021/3/9 9:26
         * @description: 查询
         */
        form.on('submit(search-submit)', function (data) {
            getPageList(1, config.pagination.limit);
            return false;
        });

        /**
         * @author: ares
         * @date: 2021/3/9 9:21
         * @description: 启用禁用
         */
        form.on('switch(status)', function (data) {
            handlerEnable($(data.elem).data('comboId'), data.elem.checked).catch(function (err) {
                data.elem.checked = !data.elem.checked;
                form.render();
            })
        });

        /**
         * @author: ares
         * @date: 2021/3/9 9:21
         * @description: 新建
         */
        $createBtn.on('click', function () {
            showModal('新建套餐')
            return false;
        })

        /**
         * @author: ares
         * @date: 2021/3/9 9:22
         * @description: 显示模态框
         * @param {string}title  显示模态框
         * @param {string}id  当前行数据id   更新操作时传入
         */
        function showModal(title, id) {
            var queryStr = (typeof id == 'string' || typeof id == 'number') ? '?id=' + id : '';
            utils.layerOpen({
                type: 2,
                title: title,
                area: ['800px', '500px'],
                content: 'pages/system/combo/form.html' + queryStr,
                end: function (index, layero) {

                }
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/9 9:23
         * @description: 更新操作
         * @param {object}data  当前行数据
         */
        function handlerUpdate(data) {
            showModal('更新套餐', data.id);
        }

        /**
         * @author: ares
         * @date: 2021/3/9 9:24
         * @description: 获取治疗单类型
         */
        function getTypes() {
            return mandyServices.mandy_get_prescriptionType_syn().then(function (res) {
                var $fragment = $(document.createDocumentFragment()),
                    data = res.data;
                typeList = data;
                $.each(data.prcp_type_list, function (idx, val) {
                    $fragment.append('<option value="' + val.id + '">' + val.name + '</option>');
                })
                $typeId.empty().append('<option value="">全部</option>').append($fragment);
                form.render('select');
            })
        }

        /**
         * @author: ares
         * @date: 2021/3/9 11:22
         * @description: 治疗项目拼接
         * @param{object} row  当前行数据
         */
        function treatmentItemFormat(row) {
           var result = '';
           $.each(row.prcpSubitemDtoList, function (idx, item) {
               result += ', '+item.itemName;
           })
            return result.substr(1);
        }

        /**
         * @author: ares
         * @date: 2021/3/9 9:44
         * @description: 分页查询
         * @param{number} currentPage
         * @param{number} pageSize
         */
        function getPageList(currentPage, pageSize) {
            comboServices.getPageList(currentPage, pageSize, form.val('search-form')).then(function (res) {
                var data = res.data,
                    list = data.list || [];
                utils.laypageRender(laypage, $.extend(config.pagination, {count: data.count, curr: data.current_page}), function (obj) {
                    getPageList(obj.curr, obj.limit);
                });

                table.render({
                    elem: '#table',
                    data: list,
                    cellMinWidth: 80,
                    cols: [[
                        {title: '套餐名称', field: 'name', width: 180},
                        {
                            title: '治疗项目',
                            templet: function (d) {
                                return treatmentItemFormat(d);
                            }
                        },
                        {title: '创建时间', field: 'createTime', width: 130},
                        {title: '操作', fixed: 'right', width: 155, templet: '#tableBar'}
                    ]]
                });
                
                table.on('tool(table)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'update':
                            handlerUpdate(data)
                            break;
                        case 'remove':
                            handlerRemove(data)
                            break;
                    };
                });
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/9 9:43
         * @description: 启用禁用套餐
         * @param {string}comboId  套餐id
         * @param {boolean}checked  是否选中
         */
        function handlerEnable(comboId, checked) {
            mandyServices.mandy_update_subitem_disabled(comboId, !checked).then(function (res) {
                utils.msgSuccess(res.message);
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/18 21:54
         * @description: 删除套餐
         * @param{object} data 当前行数据
         */
        function handlerRemove(data){
            utils.confirm('该操作将删除当前套餐, 是否继续?', {icon: 3, title: '删除套餐'}, function (index) {
                comboServices.remove(data.id).then(function (res) {
                    utils.msgSuccess(res.message);
                    getPageList(1, config.pagination.limit);
                    utils.layer.close(index);
                })
            })
        }

        /**
         * @author: ares
         * @date: 2021/3/9 9:45
         * @description: 初始化
         */
        function init() {
            getTypes().then(function () {
                getPageList(1, config.pagination.limit);
            });
        }

    })
</script>


</html>